//
//  UseRedenvelopesViewController.m
//  NewPro
//
//  Created by RYX on 2019/6/25.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "UseRedenvelopesViewController.h"
#import "UseRedenvelopesTopView.h"
#import "MyRedenvelopesStore.h"
#import "UseRedenvelopesCell.h"
#import "MyRedenvelopesModel.h"
#import "MyRedenvelopesList.h"

@interface UseRedenvelopesViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger section;
}
@property(strong, nonatomic) UITableView *tableView;
@property(strong, nonatomic) UseRedenvelopesTopView *topView;
@property(copy, nonatomic) NSArray *listArr;

@end

@implementation UseRedenvelopesViewController

- (UseRedenvelopesTopView *)topView{
    
    if (!_topView) {
        
        MJWeakSelf
        
        __block UseRedenvelopesViewController *blockSelf = self;
        _topView = [[UseRedenvelopesTopView alloc] init];
        _topView.onNoUseBtnBlock = ^{
            
            self->section = -1;
            
            [weakSelf.tableView reloadData];
            
            if (weakSelf.selectedRedBgBlock != nil) {
                
                blockSelf.selectedRedBgBlock();
            }
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    
    return _topView;
}

- (UITableView *)tableView{
 
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 9.5;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"使用红包"];
    
    section = -1;
    
    [self.view addSubview:self.topView];
    
    if ([[MyRedenvelopesList sharedInstance].selectedRedenvelopes.uid isEqualToString:@"-1"] ) {
        
        self.topView.btn.selected = YES;
    }
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(40);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(self->_topView.mas_bottom);
    }];
    
    [self loadData];
}

- (void)loadData{
    
    MJWeakSelf
    MyRedenvelopesStore *store = [[MyRedenvelopesStore alloc] init];
    [store getTicketsUserRedBgWithTicketId:self.ticketId Success:^(NSArray *arr) {
        
        weakSelf.listArr = arr;
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyRedenvelopesModel *model = self.listArr[indexPath.section];
    
    UseRedenvelopesCell *cell = [UseRedenvelopesCell tempWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = UIColor.clearColor;
    if (section == indexPath.section) {
        cell.im.image = [UIImage imageNamed:@"userRedBg_selected"];
    }else{
        cell.im.image = [UIImage imageNamed:@"userRedBg_normal"];
    }
    
    if ([[MyRedenvelopesList sharedInstance].selectedRedenvelopes.uid isEqualToString: model.uid]) {
        
        cell.im.image = [UIImage imageNamed:@"userRedBg_selected"];
    }
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [MyRedenvelopesList sharedInstance].selectedRedenvelopes = self.listArr[indexPath.section];
    
    section = indexPath.section;
    self.topView.btn.selected = NO;
    [self.tableView reloadData];
    
    if (self.selectedRedBgBlock != nil) {
        
        self.selectedRedBgBlock();
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)returnSelectedRedBgBlock:(SelectedRedBgBlock)block{
    
    self.selectedRedBgBlock = block;
}
@end
